<template>
	<view class="container" :style="{ paddingTop: getStatusBarHeight()}">
		<view class="content animate-slide-up">
			<!-- 用户信息区域 -->
			<view class="user-info-section">
				<view class="avatar-wrapper">
					<image class="avatar" :src="userInfo.avatar || ''" mode="aspectFill"></image>
				</view>
				<view class="user-details" @click="handleLogin" v-if="!isLoggedIn">
					<text class="login-text">点击登录</text>
					<text class="login-tip">登录后体验更多功能</text>
				</view>
				<view class="user-details" v-else>
					<text class="nickname">{{ userInfo.nickname || '用户昵称' }}</text>
					<text class="user-id">ID: {{ userInfo.id || '' }}</text>
				</view>
			</view>

			<!-- 功能卡片区域 -->
			<view class="function-cards">
				<view class="card-title">
					<text class="title-text">我的记录</text>
				</view>
				<view class="cards-grid">
					<view class="function-card" v-for="item in functionList" :key="item.name" @click="handleFunctionClick(item)">
						<view class="card-content">
							<uni-icons :type="item.icon" size="32" color="#36D1DC"></uni-icons>
							<text class="card-name">{{ item.name }}</text>
						</view>
					</view>
				</view>
			</view>

			<!-- 其他功能列表 -->
			<view class="other-functions">
				<view class="card-title">
					<text class="title-text">其他功能</text>
				</view>
				<view class="function-list">
					<view class="function-item" v-for="item in otherFunctions" :key="item.name"
						@click="handleFunctionClick(item)">
						<view class="function-item-left">
							<uni-icons :type="item.icon" size="24" color="#36D1DC"></uni-icons>
							<text class="function-text">{{ item.name }}</text>
						</view>
						<uni-icons type="right" size="16" color="#999"></uni-icons>
					</view>
				</view>
			</view>

			<!-- 退出登录按钮 -->
			<view class="bottom-area" v-if="isLoggedIn">
				<button class="logout-btn" hover-class="btn-hover" @click="handleLogout">
					退出登录
				</button>
			</view>
		</view>
	</view>
</template>

<script setup>
	import {
		ref
	} from 'vue'
	import {
		getStatusBarHeight
	} from '@/utils/component-api'
	import {
		onShow
	} from '@dcloudio/uni-app'
	import request from '@/utils/request.js'

	// 状态定义
	const isLoggedIn = ref(false)
	const userInfo = ref({})

	// 主要功能列表
	const functionList = ref([
		{
			name: '舌诊分析记录',
			icon: 'camera',
			path: '/pages/tongue/history',
			desc: '查看历史舌诊分析'
		},
		{
			name: '健康测评记录',
			icon: 'list',
			path: '/pages/question/history',
			desc: '查看健康测评记录'
		}
	])

	// 其他功能列表
	const otherFunctions = ref([{
			name: '个人资料',
			icon: 'person',
			path: '/pages/user/profile'
		},
		{
			name: '消息通知',
			icon: 'notification',
			path: '/pages/user/notifications'
		},
		{
			name: '系统设置',
			icon: 'settings',
			path: '/pages/user/settings'
		},
		{
			name: '用户反馈',
			icon: 'chat-filled',
			path: '/pages/user/feedback'
		}
	])

	// 方法定义
	const handleLogin = () => {
		uni.navigateTo({
			url: '/pages/login/login'
		})
	}

	const handleLogout = async () => {
		try {
			const {confirm} = await uni.showModal({
				title: '提示',
				content: '确定要退出登录吗？',
			})
			if (!confirm) {
				return
			}

			await request.post("/user/logout")

			isLoggedIn.value = false
			userInfo.value = {}
			uni.removeStorageSync('userInfo')
		} catch (e) {
			uni.showToast({
				title: e || "操作失败",
				icon: none
			})
		}
	}

	const handleFunctionClick = async (item) => {
		try {
			if (!isLoggedIn.value) {
				uni.showToast({
					title: '请先登录',
					icon: 'none'
				})
				return
			}
			await uni.navigateTo({
				url: item.path
			})
		} catch (e) {
			uni.showToast({
				title: '功能开发中',
				icon: 'none'
			})
		}
	}

	// 更新用户信息
	const updateUserInfo = () => {
		const userInfoStorage = uni.getStorageSync('userInfo')
		if (userInfoStorage) {
			isLoggedIn.value = true
			userInfo.value = userInfoStorage
		}
	}

	onShow(() => {
		updateUserInfo()
	})
</script>

<style lang="scss" scoped>
	.container {
		position: relative;
		overflow: hidden;

		&::before {
			content: '';
			position: absolute;
			top: -10%;
			right: -10%;
			width: 400rpx;
			height: 400rpx;
			background: rgba(255, 255, 255, 0.1);
			border-radius: 50%;
			filter: blur(30rpx);
		}
	}

	.content {
		padding: 40rpx;
		position: relative;
		z-index: 1;
	}

	.user-info-section {
		display: flex;
		align-items: center;
		margin-bottom: 60rpx;

		.avatar-wrapper {
			margin-right: 30rpx;

			.avatar {
				width: 120rpx;
				height: 120rpx;
				border-radius: 60rpx;
				border: 4rpx solid rgba(255, 255, 255, 0.3);
			}
		}

		.user-details {
			flex: 1;

			.login-text {
				font-size: 36rpx;
				font-weight: bold;
				display: block;
				margin-bottom: 8rpx;
			}

			.login-tip {
				font-size: 24rpx;
				opacity: 0.8;
			}

			.nickname {
				font-size: 36rpx;
				font-weight: bold;
				margin-bottom: 8rpx;
				display: block;
			}

			.user-id {
				font-size: 24rpx;
				opacity: 0.8;
			}
		}
	}

	.card-title {
		margin-bottom: 20rpx;

		.title-text {
			font-size: 32rpx;
			font-weight: bold;
			position: relative;
			padding-left: 20rpx;

			&::before {
				content: '';
				position: absolute;
				left: 0;
				top: 50%;
				transform: translateY(-50%);
				width: 6rpx;
				height: 28rpx;
				background: #FFD700;
				border-radius: 3rpx;
			}
		}
	}

	.cards-grid {
		display: flex;
		justify-content: space-between;
		gap: 20rpx;
		margin-bottom: 40rpx;
	}

	.function-card {
		flex: 1;
		background: rgba(255, 255, 255, 0.15);
		backdrop-filter: blur(10rpx);
		border-radius: 20rpx;
		padding: 24rpx 10rpx;
		transition: transform 0.3s ease;
		border: 2rpx solid rgba(200, 200, 200, 0.3);

		&:active {
			transform: scale(0.95);
		}

		.card-content {
			display: flex;
			flex-direction: column;
			align-items: center;
			gap: 12rpx;
		}

		.card-name {
			font-size: 28rpx;
			font-weight: bold;
		}
	}

	.other-functions {
		background: rgba(255, 255, 255, 0.15);
		backdrop-filter: blur(10rpx);
		border-radius: 20rpx;
		padding: 30rpx;
		margin-bottom: 40rpx;
		border: 2rpx solid rgba(200, 200, 200, 0.3);

		.function-item {
			display: flex;
			justify-content: space-between;
			align-items: center;
			padding: 20rpx 0;
			border-bottom: 1rpx solid rgba(255, 255, 255, 0.1);

			&:last-child {
				border-bottom: none;
			}

			.function-item-left {
				display: flex;
				align-items: center;

				.function-text {
					margin-left: 20rpx;
					font-size: 28rpx;
				}
			}
		}
	}

	.bottom-area {
		.logout-btn {
			width: 100%;
			height: 88rpx;
			line-height: 88rpx;
			background: rgba(255, 255, 255, 0.2);
			font-size: 32rpx;
			font-weight: bold;
			border-radius: 44rpx;
			border: 2rpx solid red;
			backdrop-filter: blur(5rpx);

			&.btn-hover {
				opacity: 0.9;
				transform: scale(0.98);
			}
		}
	}

	.animate-slide-up {
		animation: slideUp 1s cubic-bezier(0.23, 1, 0.32, 1);
	}

	@keyframes slideUp {
		from {
			transform: translateY(60rpx);
			opacity: 0;
		}

		to {
			transform: translateY(0);
			opacity: 1;
		}
	}
</style>